﻿using System;
using System.Collections.Generic;

namespace CeA.Reports.Atendimento.DataAccess.Criteria {
    [Serializable]
    public abstract class CriteriaBase {
        private DateTime m_dataInicial;
        private DateTime m_dataFinal;
        private List<int> m_regiaoIdList;
        private List<int> m_lojaIdList;
        private List<int> m_rfsIdList;
        private List<int> m_especialistaIdList;
        private List<int> m_servicoIdList;
        private bool m_chkExportRelatorio;

        public bool ChkExportRelatorio
        {
            get { return m_chkExportRelatorio; }
            set { m_chkExportRelatorio = value; }
        }
        


        /// <summary>
        /// Gets or sets the data inicial.
        /// </summary>
        /// <value>The data inicial.</value>
        public DateTime DataInicial     {
            get { return m_dataInicial; }
            set { m_dataInicial = value; }
        }

        /// <summary>
        /// Gets or sets the data final.
        /// </summary>
        /// <value>The data final.</value>
        public DateTime DataFinal       {
            get { return m_dataFinal; }
            set { m_dataFinal = value; }
        }

        /// <summary>
        /// Gets or sets the regiao m_id.
        /// </summary>
        /// <value>The regiao m_id.</value>
        public List<int> RegiaoIdList {
            get { return m_regiaoIdList; }
            set { m_regiaoIdList = value; }
        }

        /// <summary>
        /// Gets or sets the loja m_id.
        /// </summary>
        /// <value>The loja m_id.</value>
        public List<int> LojaIdList {
            get { return m_lojaIdList; }
            set { m_lojaIdList = value; }
        }

        /// <summary>
        /// Gets or sets the RFS m_id.
        /// </summary>
        /// <value>The RFS m_id.</value>
        public List<int> RfsIdList {
            get { return m_rfsIdList; }
            set { m_rfsIdList = value; }
        }

        /// <summary>
        /// Gets or sets the Especialista m_id.
        /// </summary>
        /// <value>The Especialista m_id.</value>
        public List<int> EspecialistaIdList
        {
            get { return m_especialistaIdList; }
            set { m_especialistaIdList = value; }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CriteriaBase"/> class.
        /// </summary>
        public CriteriaBase()           { 
            m_dataInicial = DateTime.MinValue;
            m_dataFinal = DateTime.MaxValue;

            m_dataInicial = new DateTime(m_dataInicial.Year, m_dataInicial.Month, m_dataInicial.Day, 0, 0, 0);
            m_dataFinal = new DateTime(m_dataFinal.Year, m_dataFinal.Month, m_dataFinal.Day, 23, 59, 59);

            m_regiaoIdList = new List<int>();
            m_lojaIdList = new List<int>();
            m_rfsIdList = new List<int>();
            m_especialistaIdList = new List<int>();
            m_servicoIdList = new List<int>();
        }


        public CriteriaBase setDataInicial(int dia, int mes, int ano)           {
            m_dataInicial = new DateTime(ano, mes, dia, m_dataInicial.Hour, m_dataInicial.Minute, m_dataInicial.Second);

            return this;
        }


        public CriteriaBase setDataFinal(int dia, int mes, int ano)             {
            m_dataFinal = new DateTime(ano, mes, dia, m_dataFinal.Hour, m_dataFinal.Minute, m_dataFinal.Second);

            return this;
        }


        public CriteriaBase setHoraInicial(int hora, int minuto, int segundo)   {
            m_dataInicial = new DateTime(m_dataInicial.Year, m_dataInicial.Month, m_dataInicial.Day, hora, minuto, segundo);

            return this;
        }


        public CriteriaBase setHoraFinal(int hora, int minuto, int segundo)     {
            m_dataFinal = new DateTime(m_dataFinal.Year, m_dataFinal.Month, m_dataFinal.Day, hora, minuto, segundo);

            return this;
        }


        public CriteriaBase setDataInicial(string ddmmyyy)                      {
            if (ddmmyyy.Length < 10)
                throw new ArgumentException("Formato de data inválido. Utilize o formato DD/MM/AAAA.");

            int[] datParts = dataParaArray(ddmmyyy);

            return setDataInicial(datParts[0], datParts[1], datParts[2]);
        }


        public CriteriaBase setDataFinal(string ddmmyyy)                        {
            if (ddmmyyy.Length < 10)
                throw new ArgumentException("Formato de data inválido. Utilize o formato DD/MM/AAAA.");

            int[] datParts = dataParaArray(ddmmyyy);

            return setDataFinal(datParts[0], datParts[1], datParts[2]);
        }
        

        public CriteriaBase setHoraInicial(string hhmmss)                       {
            if (hhmmss.Length < 8)
                throw new ArgumentException("Formato de hora inválido. Utilize o formato HH:MM:SS.");

            int[] horParts = horaParaArray(hhmmss);

            return setHoraInicial(horParts[0], horParts[1], horParts[2]);
        }


        public CriteriaBase setHoraFinal(string hhmmss)                         {
            if (hhmmss.Length < 8)
                throw new ArgumentException("Formato de hora inválido. Utilize o formato HH:MI:SS.");

            int[] horParts = horaParaArray(hhmmss);

            return setHoraFinal(horParts[0], horParts[1], horParts[2]);
        }


        public CriteriaBase  addRegiao(int id) {
            m_regiaoIdList.Add(id);
            return this;
        }


        public CriteriaBase addLoja(int id) {
            m_lojaIdList.Add(id);
            return this;
        } 


        public CriteriaBase addRfs(int id) {
            m_rfsIdList.Add(id);
            return this;
        }

        public CriteriaBase addEspecialista(int id)
        {
            m_especialistaIdList.Add(id);
            return this;
        }

        public CriteriaBase addServicoIdList(int id)
        {
            m_servicoIdList.Add(id);
            return this;
        }


        /// <summary>
        /// Converte uma data no formato DD/MM/YYYY para suas respectivas partes.
        /// int[0]=DIA int[1]=MES int[2]=ANO
        /// </summary>
        /// <param name="ddmmyyy">Data no formato dd/mm/yyyy.</param>
        /// <returns>int[] contendo as respectivas partes da data.</returns>
        private int[] dataParaArray(string ddmmyyy)                             {
            string[] datPart = ddmmyyy.Split('/');

            int[] part = new int[] {
                int.Parse(datPart[0]), int.Parse(datPart[1]), int.Parse(datPart[2])
            };

            return part;
        }

        /// <summary>
        /// Converte um horário no formato HH:MM:SS para suas respectivas partes.
        /// int[0]=HORA int[1]=MINUTO int[2]=SEGUNDO
        /// </summary>
        /// <param name="ddmmyyy">Data no formato dd/mm/yyyy.</param>
        /// <returns>int[] contendo as respectivas partes da data</returns>
        private int[] horaParaArray(string hhmmss)                              {
            string[] horPart = hhmmss.Split(':');
            
            int[] part = new int[] {
                int.Parse(horPart[0]), int.Parse(horPart[1]), int.Parse(horPart[2])
            };

            return part;
        }
    }
}
